
#import "GetTorrentPeerListRequest.h"

@implementation GetTorrentPeerListRequest

//
// torrent id
//
@synthesize torrentId														= _torrentId;

//
// seeder list
//
@synthesize seederList														= _seederList;

//
// leecher list
//
@synthesize leecherList														= _leecherList;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super init];
	if(!self)
		return nil;
	
	//
	// init xml parser
	//
	self.xmlParser															= [[[XmlParser alloc] init] autorelease];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.seederList															= nil;
	self.leecherList														= nil;
	
	[super dealloc];
}

//
// setup request
//
- (void)setupRequest
{
	//
	// setup relpath
	//
	self.relPath															= [NSString stringWithFormat:@"viewpeerlist.php?id=%d", self.torrentId];
	
	//
	// call super
	//
	[super setupRequest];
}

//
// finished
//
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
	if(self.response.statusCode == 200)
	{
		NSMutableData* fullData												= [NSMutableData dataWithData:self.dataBuffer];
		static char const* header											= "<html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> </head><body>";
		static char const* footer											= "</body></html>";
		[fullData replaceBytesInRange:NSMakeRange(0, 0) withBytes:header length:strlen(header)];
		[fullData appendBytes:footer length:strlen(footer)];
		
		[self.xmlParser parseDocumentData:fullData htmlMode:YES];
		NSString* temp														= [self.xmlParser stringFromPath:@"/html/body/b[1]" startNode:0];
		temp																= [temp stringByReplacingOccurrencesOfString:@"Seeders" withString:@""];
		NSInteger seederCount												= [temp integerValue];
		self.seederList														= [NSMutableArray arrayWithCapacity:seederCount];
		temp																= [self.xmlParser stringFromPath:@"/html/body/b[2]" startNode:0];
		temp																= [temp stringByReplacingOccurrencesOfString:@"Leechers" withString:@""];
		NSInteger leecherCount												= [temp integerValue];
		self.leecherList													= [NSMutableArray arrayWithCapacity:leecherCount];
		NSInteger tableCount												= (seederCount ? 1 : 0) + (leecherCount ? 1 : 0);
		for(int i = 0; i < tableCount; i ++)
		{
			NSArray* peerRows												= [self.xmlParser nodesFromPath:[NSString stringWithFormat:@"/html/body/table[%d]/tr[position()>1]", i + 1] startNode:0];
			for(NSValue* rowValue in peerRows)
			{
				@autoreleasepool
				{
					xmlNodePtr xmlRow										= (xmlNodePtr)[rowValue pointerValue];
					TorrentPeerInfo* info									= [[[TorrentPeerInfo alloc] init] autorelease];
					info.userName											= [self.xmlParser stringFromPath:@"./td[1]/nobr/span[1]/a/b" startNode:xmlRow];
					if(!info.userName.length)
					{
						info.userName										= [self.xmlParser stringFromPath:@"./td[1]/nobr/i" startNode:xmlRow];
						info.ipType											= [[self.xmlParser stringFromPath:@"./td[1]/nobr" startNode:xmlRow] substringFromIndex:info.userName.length];
						info.userId											= 0;
					}
					else
					{
						info.ipType											= [[self.xmlParser stringFromPath:@"./td[1]/nobr" startNode:xmlRow] substringFromIndex:info.userName.length];
						temp												= [self.xmlParser stringFromPath:@"./td[1]/nobr/span/a/@href" startNode:xmlRow];
						info.userId											= [[temp substringFromIndex:19] integerValue];
					}
					
					info.uploadedSize										= [self.xmlParser stringFromPath:@"./td[2]" startNode:xmlRow];
					info.avgUploadRate										= [self.xmlParser stringFromPath:@"./td[3]" startNode:xmlRow];
					info.uploadRate											= [self.xmlParser stringFromPath:@"./td[4]" startNode:xmlRow];
					info.downloadedSize										= [self.xmlParser stringFromPath:@"./td[5]" startNode:xmlRow];
					info.avgDownloadRate									= [self.xmlParser stringFromPath:@"./td[6]" startNode:xmlRow];
					
					if(i == 0 && seederCount)
					{
						info.downloadRate									= @"---";
						info.shareRatio										= NSLocalizedString([self.xmlParser stringFromPath:@"./td[7]" startNode:xmlRow], nil);
						info.completed										= @"100%";
						info.connectedTime									= [NSString formatTime:[[self.xmlParser stringFromPath:@"./td[8]" startNode:xmlRow] decodeTime]];
						info.idleTime										= [NSString formatTime:[[self.xmlParser stringFromPath:@"./td[9]" startNode:xmlRow] decodeTime]];
						info.client											= [self.xmlParser stringFromPath:@"./td[10]" startNode:xmlRow];
						[self.seederList addObject:info];
					}
					else
					{
						info.downloadRate									= [self.xmlParser stringFromPath:@"./td[7]" startNode:xmlRow];
						info.shareRatio										= NSLocalizedString([self.xmlParser stringFromPath:@"./td[8]" startNode:xmlRow], nil);
						info.completed										= [self.xmlParser stringFromPath:@"./td[9]" startNode:xmlRow];
						info.connectedTime									= [NSString formatTime:[[self.xmlParser stringFromPath:@"./td[10]" startNode:xmlRow] decodeTime]];
						info.idleTime										= [NSString formatTime:[[self.xmlParser stringFromPath:@"./td[11]" startNode:xmlRow] decodeTime]];
						info.client											= [self.xmlParser stringFromPath:@"./td[12]" startNode:xmlRow];
						[self.leecherList addObject:info];
					}
				}
			}
		}
	}
	
	[super connectionDidFinishLoading:connection];
}

@end
